<%@ page pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="isperp" uri="/WEB-INF/tlds/isperp.tld"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<style>
<!--
#customerCiCategoryMenu {
	padding: 0 0 10px 0 !important;
}

#customerCiCategoryMenu li a.curSelectedNode {
	border: 1px solid #FFB951 !important;
}
-->
</style>
<script type="text/javascript">
	function customerCiCategoryMenu_onNodeClick(event, treeId, treeNode) {
		
		$("#ciRelationType").val("");
		$.getJSON("configuration/category/property/listByCategory", {
			categoryId : treeNode.id
		}, function(data) {
			if (data && data[0]) {
				var options = '<option value=""></options>';
				$(data).each(function(index, item) {
					options += "<option value='"+item.name+"' data-items='"+item.items+"' data-datatype='"+item.data_type+"'>" + item.name + "</option>";
				});

				$("#ciProperty1,#ciProperty2,#ciProperty3,#ciProperty4,#ciProperty5,#ciProperty6").html(options);
				$("#ciProperty1,#ciProperty2,#ciProperty3,#ciProperty4,#ciProperty5,#ciProperty6").trigger("chosen:updated");
			}
		});

		$("#ciPropertCondition").show();
		getSearchConditionAndSearch(treeNode.id);
	}

	function intCiPropertyDrp(categoryId) {
		$.getJSON("configuration/category/property/listByCategory", {
			categoryId : categoryId
		}, function(data) {
			if (data && data[0]) {
				var options = '<option value=""></options>';
				$(data).each(function(index, item) {
					options += "<option value='"+item.name+"' data-items='"+item.items+"' data-datatype='"+item.data_type+"'>" + item.name + "</option>";
				});

				$("#ciProperty1,#ciProperty2,#ciProperty3,#ciProperty4,#ciProperty5,#ciProperty6").html(options);
				$("#ciProperty1,#ciProperty2,#ciProperty3,#ciProperty4,#ciProperty5,#ciProperty6").trigger("chosen:updated");
			}
		});
	}

	function customerCiCategoryMenu1_onNodeClick(event, treeId, treeNode) {

		intCiPropertyDrp(treeNode.id);
		$("#ciRelationType").val("");
		$.getJSON("configuration/ciRelationType/listByCategory", {
			categoryId : treeNode.id
		}, function(data) {
			if (data && data[0]) {
				var options = '<option value=""></options>';
				$(data).each(function(index, item) {
					options += "<option value='"+item.id+"' data-items='"+item.items +"' data-datatype='"+item.data_type+"'>" + item.from_category_alias_name + ' ->' + item.to_category_alias_name + "</option>";
				});

				$("#ciRelationType").html(options);
				$("#ciRelationType").trigger("chosen:updated");
			} else {
				$("#ciRelationType").html("");
				$("#ciRelationType").trigger("chosen:updated");
			}
		});
		
		$("#ciPropertCondition").show();
		getSearchConditionAndSearch();
	}

	function categoryCiGrid_beforeCreateAction() {
		var customerId = $("#customerId").val();
		if (!customerId || customerId == "") {
			alert("请先选择客户信息！");
			return false;
		}

		var param = "customerId=" + customerId;

		var categoryNode = customerCiCategoryMenu1.getSelectedNode();
		if (categoryNode) {

			param += "&categoryId=" + categoryNode.id;
		}

		return param;

	}

	function changeShowMore(obj) {

		var $moreDiv = $("#moreDiv");
		var $obj = $(obj);
		var d = $moreDiv.css("display");

		if (d == "none" || d == undefined || d == "") {
			var d = $moreDiv.css("display", "block");

			$obj.removeClass("unfold");
			$obj.addClass("fold");
		} else {
			var d = $moreDiv.css("display", "none");

			$obj.removeClass("fold");
			$obj.addClass("unfold");
		}

		intCiPropertyDrp("");

	}
</script>
<div id="dvFrameMainNav" class="frame-main-nav">

	<isperp:tree id="customerCiCategoryMenu1" modal="false" title="资产分类">
		<isperp:async enable="true" autoParam="'id'"
			otherParam="{asyncLoad:1}" url="configuration/category/getTree">
		</isperp:async>
		<isperp:callback onClick="customerCiCategoryMenu1_onNodeClick">
		</isperp:callback>
		<isperp:view nameIsHTML="true"></isperp:view>
	</isperp:tree>

</div>
<div id="mainContent" class="frame-main-cont frame-main-cont-first"
	role="main" style="top:20px;">
	<div id="dvContainer">
		<div class="f-t1"
			style="float:left;padding:3px 10px 3px 20px;color:#666;font-size: 12px;font-weight: bold;">1.选择客户</div>
		<div class="ft2" style="">
			<select id="customerId" class="search" data-width="260px">
			   <c:if test="${customerCiSize ne 1 }">
			      <option value="">所有</option>
			   </c:if>
				<c:forEach items="${customerCiCounts }" var="customerCiCount">
					<option value="${customerCiCount.id }">${customerCiCount.name}(${customerCiCount.count_id})</option>
				</c:forEach>
			</select>
		</div>
		<br>
		<div class="m m7">
			<div class="mt">
				<h3 style="cursor: pointer;" onclick="changeShowMore(this)">
					更多属性查询<span option="more" class="o-more" style=""><b></b> </span>
				</h3>
			</div>
			<div class="mc" id="moreDiv" style="display:none">

				<table class="search-table" id="categoryCiGridAdSearchTb">
					
					<tr>
						<td class="td1">配置关系：</td>
						<td colspan="4"><select id="ciRelationType" data-width="65%"
							data-allow-empty="true">
						</select><font>(查询与该类资产具有关联属性的资产)</font></td>
					</tr>
					<tr>
						<td class="td1">编号：</td>
						<td><input type="text" field="code" op="cn">
						</td>
						<td class="td1">名称：</td>
						<td><input type="text" field="name" op="cn"></td>
						<td class="td1">资产状态：</td>
						<td><form:select path="var_assetStatus" field="status"
								id="SassetStatus" data-allow-empty="true" op="cn"
								items="${var_assetStatus}" itemLabel="name" itemValue="name" />
						</td>
					</tr>

					<tr>
						<td class="td1">使用人：</td>
						<td><input type="text" field="use_staff_name" op="cn">
						<td class="td1">位置：</td>
						<td><input type="text" field="location_name" op="cn">
						</td>

						<td class="td1">备注：</td>
						<td class="td2" colspan="3"><input type="text" field="meno"
							op="cn">
						</td>
					</tr>

					<tr>
						<td class="td1">报废日期：</td>
						<td class="td2"><input id="expiredDateStart" type="date"
							field="expired_date" op="gt" style="width: 68px" />~<input
							id="expiredDateEnd" name="expiredDate" type="date"
							field="expired_date" op="lt" style="width: 68px" />
						</td>
						<td class="td1">序列号：</td>
						<td colspan="3"><input type="text" style="width: 298px"
							field="asset_code" op="in"
							placeholder="多个序列号搜索可用逗号（,）隔开，如（SN01,SN02）">
						</td>
						<!-- <td class="td1" colspan="2"><a class="nui-txt-link"
							href="javascript:btncategoryCiGridSearchReset();">清除查询条件</a>
						</td> -->
					</tr>
				</table>
				<table class="search-table" id="categoryCiGridAdSearchTb">
					<tr>
						<td colspan="6">*自定义属性(使用自定义属性搜索，请先选择资产分类)</td>
					</tr>

					<tr>
						<td class="td1">第一组：</td>
						<td class="td1" style="text-align: left"><select
							id="ciProperty1" data-width="200px">
						</select></td>
						<td class="td2" colspan="2"><select id="ciProperty1Filter">
								<option value="eq">=</option>
								<option value="cn">包含</option>
								<option value="gt">大于</option>
								<option value="lt">小于</option>
						</select> <span id="ciProperty1Value" style="vertical-align: middle;"></span>
						</td>
						<td class="td1">第二组：</td>
						<td class="td1" style="text-align: left"><select
							id="ciProperty2"></select></td>
						<td class="td2" colspan="2"><select id="ciProperty2Filter">
								<option value="eq">=</option>
								<option value="cn">包含</option>
								<option value="gt">大于</option>
								<option value="lt">小于</option>
						</select> <span id="ciProperty2Value" style="vertical-align: middle;"></span>
						</td>
					</tr>
					<tr>
						<td class="td1">第三组：</td>
						<td class="td1" style="text-align: left"><select
							id="ciProperty3"></select></td>
						<td class="td2" colspan="2"><select id="ciProperty3Filter">
								<option value="eq">=</option>
								<option value="cn">包含</option>
								<option value="gt">大于</option>
								<option value="lt">小于</option>
						</select> <span id="ciProperty3Value" style="vertical-align: middle;"></span>
						</td>

						<td class="td1">第四组：</td>
						<td class="td1" style="text-align: left"><select
							id="ciProperty4"></select></td>
						<td class="td2" colspan="2"><select id="ciProperty4Filter">
								<option value="eq">=</option>
								<option value="cn">包含</option>
								<option value="gt">大于</option>
								<option value="lt">小于</option>
						</select> <span id="ciProperty4value" style="vertical-align: middle;"></span>
						</td>


					</tr>
					<tr>
						<td class="td1">第五组：</td>
						<td class="td1" style="text-align: left"><select
							id="ciProperty5"></select></td>
						<td class="td2" colspan="2"><select id="ciProperty5Filter">
								<option value="eq">=</option>
								<option value="cn">包含</option>
								<option value="gt">大于</option>
								<option value="lt">小于</option>
						</select> <span id="ciProperty5Value" style="vertical-align: middle;"></span>
						</td>

						<td class="td1">第六组：</td>
						<td class="td1" style="text-align: left"><select
							id="ciProperty6"></select></td>
						<td class="td2" colspan="2"><select id="ciProperty6Filter">
								<option value="eq">=</option>
								<option value="cn">包含</option>
								<option value="gt">大于</option>
								<option value="lt">小于</option>
						</select> <span id="ciProperty6value" style="vertical-align: middle;"></span>
						</td>


					</tr>

				</table>
			</div>
		</div>




		<isperp:grid gridId="categoryCiGrid" url="configuration/ci/listCi"
			adSearchTb="categoryCiGridAdSearchTb" datatype="local"
			cusFormatterActions="true" showSearchbar="false"
			cusEditUrl="configuration/ci/edit" editOrDeleteRightControl="true"
			cusDeleteUrl="configuration/ci/delete" actionsWidth="100"
			cusViewUrl="configuration/ci/edit?readonly=true"
			excelExportUrl="incident/incident/excelExport"
			beforeCreateAction="categoryCiGrid_beforeCreateAction()"
			ondbClickRowEvent="categoryCiGrid.edit(rowid);">

			<jsp:attribute name="toolbar">
			<c:if test="${showAddBtn eq '1' }">
			    <isperp:button id="categoryCiCreate" icon="icon-plus" text="新增资产项"
					cssClass="nui-mainbtn" onclick="categoryCiGrid.create();"></isperp:button>
			</c:if>			
	        <isperp:button id="categoryCiExport" icon="icon-download"
			text="导出到Excel"
			onclick="excelExportCi();"></isperp:button>
					
	<isperp:button id="categoryCreate" icon="icon-search" text="查询"
					onclick="getSearchConditionAndSearch();"></isperp:button>
	</jsp:attribute>
	<jsp:body>
	<isperp:column name="id" title="id" hidden="true"></isperp:column>
	<isperp:column name="code" title="编号" width="90">
	</isperp:column>
	<isperp:column name="name" title="名称" width="150">
	</isperp:column>
	<isperp:column name="category_name" title="资产分类" width="110"
					>
	</isperp:column>
	<isperp:column name="status" title="状态" width="60">
	</isperp:column>
	<isperp:column name="cus_customer_name" title="所属客户" width="60"
					>
	</isperp:column>
	<isperp:column name="department_name" title="部门" width="80"
					hidden="true">
	</isperp:column>
	<isperp:column name="use_staff_name" title="责任人" width="60" hidden="true">
	</isperp:column>
	<isperp:column name="management_staff_name" title="管理者" width="60"
					hidden="true">
	</isperp:column>
	<isperp:column name="location_name" title="位置" width="100">
	</isperp:column>
	<isperp:column name="address" title="地址" width="100" hidden="true">
	</isperp:column>
	<isperp:column name="meno" title="备注" width="100" hidden="true">
	</isperp:column>
	<isperp:column name="specification" title="规格" width="100"
					hidden="true">
	</isperp:column>
	<isperp:column name="model" title="型号" width="100" hidden="true">
	</isperp:column>
	<isperp:column name="provider" title="供应商" width="100" hidden="true">
	</isperp:column>
	<isperp:column name="manufacturer_provider_name" title="制造商"
					width="100" hidden="true">
	</isperp:column>
	<isperp:column name="maintainer_provider_name" title="维保商" width="100"
					hidden="true">
	</isperp:column>
	<isperp:column name="support_staff_name" title="维护者" width="100"
					hidden="true">
	</isperp:column>
	<isperp:column name="repair_tel" title="保修电话" width="100" hidden="true">
	</isperp:column>
	<isperp:column name="price" title="价格" width="100" hidden="true">
	</isperp:column>
	<isperp:column name="asset_code" title="序列号" width="100">
	</isperp:column>
	<isperp:column name="buy_date" title="采购日期" hidden="true" width="100">
	</isperp:column>
	<isperp:column name="receive_date" title="接收日期" hidden="true"
					width="100">
	</isperp:column>
	<isperp:column name="installation_date" title="安装日期" hidden="true"
					width="100">
	</isperp:column>
	<isperp:column name="available_date" title="在用日期" hidden="true"
					width="100">
	</isperp:column>
	<isperp:column name="expired_date" title="报废日期" hidden="true"
					width="100">
	</isperp:column>
	<isperp:column name="maintainer_start_date" title="维保开始日期" hidden="true"
					width="100">
	</isperp:column>
	<isperp:column name="maintainer_end_date" title="维保结束日期"
					isFormatterFun="true" hidden="true"
					formatter="return ciDateFormatter(cellvalue,1);" width="120">
	</isperp:column>
	<isperp:column name="from_relation_count" title="关联资产" width="80">
	</isperp:column>
	<isperp:column name="to_relation_count" title="被关联数" width="80">
	</isperp:column>
	
	
       </jsp:body>
	</isperp:grid>
	</div>
</div>
<%-- <isperp:operate code="CI_FILE"></isperp:operate> --%>
<script type="text/javascript">
	$(document).ready(function() {
		//customerCiCategoryMenu.zTree.setting.async.url = "configuration/ci/getCustomerCiCategoryMenu?customerId=" + $("#customerId").val();
		//customerCiCategoryMenu.reload();
		$("#ciRelationType").html("");
		$("#ciRelationType").trigger("chosen:updated");
		$("#SassetStatus").val("");
		$("#var_assetStatus").val("");
		$("#var_assetStatus").trigger("chosen:updated");
		getSearchConditionAndSearch();

	});
	//--------资产导出----------//
	function excelExportCi() {
	    /* var gsize = categoryCiGrid.jqGrid.getGridParam("reccount");
	    if(gsize==0)
	    {
	       isperp.alert("没有数据导出！");
		   return false;
	    } */
		var excelExportUrl = categoryCiGrid.jqGrid.getGridParam("url");
		if (excelExportUrl == "") {
			isperp.alert("请配置导出路径！");
			return false;
		}

		var colModel = categoryCiGrid.jqGrid.getGridParam("colModel"), eocolName = '';
		$(colModel).each(function(index, item) {
			if (item.name != "rn")
				eocolName += "," + item.label
		});
		
		 var realExcelExportUrl = "";
		var categoryId=getQueryString(excelExportUrl,"categoryId");
		 if(categoryId!="" && categoryId!=null && categoryId!=undefined)
		{
		  svExcelExportUrl="/exportExcel/exportCiExcel";
		}else
		{
		  svExcelExportUrl="/exportExcel";
		}
		 categoryCiGrid.jqGrid.jqGrid("excelExport", {
			url : isperp.webPath + svExcelExportUrl,
			exportOptions : eocolName.substring(1) + ";" + excelExportUrl
		});  
		return false;
	}

	function getQueryString(url,name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = url.substr(1).match(reg);
		if (r != null)
			return unescape(r[2]);
		return null;
	}
	//--------资产导出 END----------//

	$("#customerId").change(function() {

		$("#ciPropertCondition").hide();
		//customerCiCategoryMenu.zTree.setting.async.url = "configuration/ci/getCustomerCiCategoryMenu?customerId=" + $(this).val();
		//customerCiCategoryMenu.reload();
		getSearchConditionAndSearch();
	});

	$("#ciProperty1,#ciProperty2,#ciProperty3,#ciProperty4,#ciProperty5,#ciProperty6").chosen({
		width : "150px",
		allow_single_deselect : true
	}).change(function() {

		var o = $(this).find("option:selected");
		var items = o.data("items");
		var valueSpan = $(this).attr("id") + "Value";
		if (items && items != "undefined") {
			var controlStr = '<select id="'+valueSpan+'Control"><option value=""></option>';

			var itemArr = items.split("|");

			$(itemArr).each(function(i, it) {
				controlStr += "<option value='"+it+"'>" + it + "</option>";
			});
			controlStr += "</select>";
			$("#" + valueSpan).html(controlStr);

			$("#" + valueSpan + "Control").chosen({
				width : "155px",
				allow_single_deselect : true,
				disable_search : true
			});

		} else {
			var dtype = o.data("datatype");
			$("#" + valueSpan).html("<input  id='"+valueSpan+"Control' type='"+dtype+"'>");
			isperp.initElement("#" + valueSpan);
		}

	});
	$("#ciProperty1Filter,#ciProperty2Filter,#ciProperty3Filter,#ciProperty4Filter,#ciProperty5Filter,#ciProperty6Filter").chosen({
		width : "70px",
		disable_search : true
	});

	var categoryCiGridColModel = categoryCiGrid.jqGrid.getGridParam("colModel"), categoryCiGridColNames = categoryCiGrid.jqGrid.getGridParam("colNames");

	function getSearchConditionAndSearch(treeNodeId) {

		var f = getSearchCondition(treeNodeId);

		categoryCiGrid.jqGrid.setGridParam({
			url : "configuration/ci/listCi?" + f
		});
		categoryCiGrid.search();

	};

	function getSearchCondition(treeNodeId) {

		var newcategoryCiGridColNames = categoryCiGridColNames.slice(1, categoryCiGridColNames.length - 2);
		var newcategoryCiGridColModel = categoryCiGridColModel.slice(1, categoryCiGridColModel.length - 2);

		var f = "customerId=" + $("#customerId").val();
		var ciRelationType = $("#ciRelationType").val();
		if (ciRelationType != null && ciRelationType != "") {
			f += "&ciRelationType=" + $("#ciRelationType").val();
		}
		if (customerCiCategoryMenu1.getSelectedNode()) {
			if (treeNodeId == null || treeNodeId == undefined) {
				var tn = customerCiCategoryMenu1.getSelectedNode();
				f += "&categoryId=" + tn.id + "&categoryNamePath=" + tn.treenodeNamePath;
			}
		}

		resetColumn = false;
		$("#ciProperty1,#ciProperty2,#ciProperty3,#ciProperty4,#ciProperty5,#ciProperty6").each(function(index, item) {
			var o = $(item).find("option:selected");
			var oVal = $(o).val();
			if (oVal) {
				var iId = $(item).attr("id");

				var oValue = $("#" + iId + "ValueControl").val();

				if (oValue) {
					var iIndex = index + 1;
					f += "&p" + iIndex + "=" + oVal + "&p" + iIndex + "op=" + $("#" + iId + "Filter").val() + "&p" + iIndex + "value=" + encodeURIComponent(oValue);
					newcategoryCiGridColNames.push($(o).text());
					newcategoryCiGridColModel.push({
						label : $(o).text(),
						name : "p" + iIndex,
						width : '80'
					});
					resetColumn = true;
				}

			}
		});

		categoryCiGrid.jqGrid.GridUnload();
		categoryCiGrid.jqGrid = categoryCiGrid.createGrid(newcategoryCiGridColNames, newcategoryCiGridColModel);

		return f;

	}

	var nowDate = new Date();

	function ciDateFormatter(cellvalue, type) {
		var title = "";

		if (cellvalue) {

			var cellDate = Date.toDate(cellvalue);
			if (cellDate < nowDate) {
				if (type == 1) {
					title = "设备已过报废期！";
				} else {
					title = "设备已过维护期！";
				}
				return cellvalue + " <img title='"+ title +"' src='img/ci/overtime.gif'>";
			} else {
				return cellvalue;
			}

		}

		return "";

	};
	//故障履历
	function incidentHisFormatter(cellvalue, id) {
		if (cellvalue > 0) {
			return "<a class=\"nui-txt-link\" href=\"javascript:openCIResume('" + id + "',1);\">" + cellvalue + "</a>";
		} else {
			return "<a class=\"nui-txt-link\" href=\"javascript:void(0);\">" + cellvalue + "</a>";
		}
	};
	//变更履历
	function changeHisFormatter(cellvalue, id) {
		if (cellvalue > 0) {
			return "<a class=\"nui-txt-link\" onclick='openCIResume(" + id + ",2)'>" + cellvalue + "</a>";
		} else {
			return "<a class=\"nui-txt-link\" onclick='void(0)'>" + cellvalue + "</a>";
		}
	};

	function openCIResume(id, type) {
		var tempTitle;
		var tempUrl;
		if (type == 1) {
			tempTitle = "资产履历";
			tempUrl = "configuration/ci/ciResumeView?id=" + id;
		} else if (type == 2) {
			tempTitle = "资产履历";
			tempUrl = "configuration/ci/ciFailureResumeView.jsp?id=" + id;
		}
		var options = {
			title : tempTitle,
			contentUrl : tempUrl
		};
		isperp.openDialog(options);
	}
</script>


<style>
<!--
.fixed {
	position: fixed;
}

#dvFrameMainNav {
	overflow: auto
}
-->
</style>
<script type="text/javascript">
	var getScrollY = function() {
		scrOfY = 0;
		if (typeof (window.pageYOffset) == "number") {
			scrOfY = window.pageYOffset;
		} else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
			scrOfY = document.body.scrollTop;
		} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
			scrOfY = document.documentElement.scrollTop;
		}
		return scrOfY;

	}

	var pos_fixed_start_y = 120;
	var pos_fixed_stop_y = $(document).height() - $(window).height() - 150;
	$(window).scroll(function() {

		var y = getScrollY();

		if (y == 0) {
			$("#dvFrameMainNav").css({
				height : 'auto'
			});
		}
		var nav = $('#dvFrameMainNav');
		if (y >= pos_fixed_start_y && y <= pos_fixed_stop_y) {
			nav.addClass('fixed');
			resetTagTreeHeight();
		} else if (y > pos_fixed_stop_y) {
			nav.addClass('fixed');
			resetTagTreeHeight2();
		} else {
			nav.removeClass('fixed');
		}
	});

	var resetTagTreeHeight = function() {
		var tt = $("#dvFrameMainNav");
		var height = $(window).height();
		tt.css({
			height : height - 20
		});
	};

	var resetTagTreeHeight2 = function() {
		var tt = $("#dvFrameMainNav");
		var height = $(window).height();
		tt.css({
			height : height - 20
		});
	};

	resetTagTreeHeight2();
</script>

